//
//  File.cpp
//  rtmiditests
//
//  Created by James O'Neill on 2/14/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#ifndef OSCINTERFACE_H
#define OSCINTERFACE_H

#include <iostream>
#include "GrooveInfo.h"

#include "osc/OscOutboundPacketStream.h"
#include "ip/UdpSocket.h"

#define ERROR(s) cerr << s << "\n" << "OSC data not sent\n"; return false

#define ADDRESS "143.215.102.181"
#define PORT 11000
#define OUTPUT_BUFFER_SIZE 1024

class OscInterface {

    
public:
    
    void sendOscData(GrooveInfo *info) {

        //if (!checkGrooveInfo(info)) return;
        
        UdpTransmitSocket transmitSocket( IpEndpointName( ADDRESS, PORT ) );

        char buffer[OUTPUT_BUFFER_SIZE];
        osc::OutboundPacketStream p( buffer, OUTPUT_BUFFER_SIZE );

        p << osc::BeginMessage( "/groove" ) ;
        
        p << info->currentGroove;
        p << info->predGroove;
        
        for (int i = 0; i < 19; ++i) {
            
            p << info->predDist[i];
            
        }
        
        /*
        for (int i = 0; i < info->labels.size(); ++i) {
            p << info->labels[i]; 
        }
        */
        /*
        
        for (int i = 0; i < info->distributions.size(); ++i) {
            for (int j = 0; j < info->distributions[0].size(); ++j) {
                p << info->distributions[i][j];
            }
        }
        
        for (int i = 0; i < info->features.size(); ++i) {
            for (int j = 0; j < info->features[0].size(); ++j) {
                p << info->features[i][j];
            }
        }

         */
        p << osc::EndMessage;
        //p << true << 23 << (float)3.1415 << "hello" << osc::EndMessage;

        transmitSocket.Send( p.Data(), p.Size() );
    
    }
    
private:
    
    bool checkGrooveInfo(GrooveInfo *info) {
        
		/*
        for (int i = 0; i < info->labels.size(); ++i) {
            if (info->labels[i] == -1) 
                ERROR("Invalid label");
        }
        
        for (int i = 0; i < info->distributions.size(); ++i) {
            for (int j = 0; j < info->distributions[0].size(); ++j) {
                if (info->distributions[i][j] == -1.0) 
                    ERROR("Invalid distribution");
            }
        }
        
        for (int i = 0; i < info->features.size(); ++i) {
            for (int j = 0; j < info->features[0].size(); ++j) {
                if (info->features[i][j] == -1.0) 
                    ERROR("Invalid feature");
            }
        }
		*/
        return true;
    }
    
};

#endif

